package {
	import com.debug.Debug;
	import com.font.FontEmbed;
	import com.font.FontURL;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.system.LoaderContext;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	import org.adobe.basecode.BitmapBytes;
	import org.adobe.basecode.PNGDecoder;
	
	/**
	 * var bd:BitmapData = PNGDecoder.decodeImage(myByteArray);
	 * var xml:XML = PNGDecoder.decodeInfo(myByteArray);
	*/
	
	[SWF(backgroundColor=0xdddddd,width=550,height=450)]
	public class FontJSON extends Sprite {
		private var loader:URLLoader;
		private var _loader:Loader;
		
		public function FontJSON() {
			/*_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
			_loader.load(new URLRequest("icon.png"));*/
			//testURLStream();
			
			/*for (var i:int = 0; i < 4; i++) {
				trace("abacd" + "\t" + "b");
				trace("d" + "\t" + "b");
			}*/
			
			//showimage();
			//Demo();
			var fonturl:FontURL = new FontURL();
			var format:TextFormat = new TextFormat();
			format.size = 14;
			format.font = fonturl.getFontName();
			
			var txt:TextField = new TextField();
			txt.text = "华康海报";
			txt.embedFonts = true;
			txt.setTextFormat(format);
			this.addChild(txt);
		}
		
		private var txt:TextField;
		private var fontLoader:Loader;
		
		public function Demo():void {
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.textColor = 0x0099FF;
			txt.y = 10;
			addChild(txt);
			fontLoader = new Loader();
			fontLoader.load(new URLRequest("TestEnbed.swf"), new LoaderContext(false, loaderInfo.applicationDomain));
			fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			fontLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
		
		private function progressHandler(e:ProgressEvent):void {
			var ratio:int = e.bytesLoaded / e.bytesTotal * 100;
			//trace("加载中:" + ratio + "%");
			txt.text = "加载：" + ratio + "%";
		}
		
		private function completeHandler(e:Event):void {
			fontLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
			fontLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
			//trace("font 加载完成!");
			
			var myfont:Class = getDefinitionByName("Comic1") as Class;
			var myfont1:Class = getDefinitionByName("Comic3") as Class;
			//var my:FontEmbed = new FontEmbed(myfont1);
			//trace("嵌入字体:" + my.fontName);
			FontEmbed.getInstance().setFontClass(myfont,"中文");
			FontEmbed.getInstance().setFontClass(myfont1,"像素字");
			
			var font1:String = FontEmbed.getInstance().getFontName("中文")
			var font2:String = FontEmbed.getInstance().getFontName("像素字")
			trace("嵌入字体:" + font1);
			trace("嵌入字体:" + font2);
			
			var arr:Array = Font.enumerateFonts();
			for each(var item:* in arr){
				trace(Font(item).fontName);
			}
			
			var format:TextFormat = new TextFormat(font1, 12);
			txt.defaultTextFormat = format;
			
			//设置嵌入字体
			txt.embedFonts = true;
			txt.text = "设置嵌入字体";
		}
		
		private function byteHandler():void{
			var json:String = "";
			var bytes:ByteArray = new ByteArray();
			bytes.writeUTF(json);
		}
		
		private function formatJSON(s:String):String{
			var str:String=s;
			var pattern:RegExp = /'/g;       //替换单引号
			str=str.replace(pattern,"\"");  
			pattern=/ /g;					//空格
			str=str.replace(pattern,"");
			
			pattern=/\{([^"^:]*):/g;
			str=str.replace(pattern,"{$1:"); //$1分组

			pattern=/,([^"^:]*):/g;
			str=str.replace(pattern,",$1:");
			return str;
		}

		private function showimage():void {
			loadPNGFile("test.png");
		}   
		/**使用URLLoader二进制加载图片数据*/
		private function loadPNGFile( url:String ):void {
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY; 
			loader.addEventListener( Event.COMPLETE, onCompleteLoad );
			loader.load( new URLRequest( url ) );
		}
		private function onCompleteLoad( e:Event ):void {
			var byte:ByteArray = e.target.data as ByteArray;
			
			//将二进制数据转换成BitmapData
			var bd:BitmapData = PNGDecoder.decodeImage(byte);
			var myBitmap:Bitmap = new Bitmap(bd,"auto",true);
			this.addChild(myBitmap);
		}
		
		private function testURLStream():void   {   
			var stream:URLStream = new URLStream();   
			
			stream.addEventListener(ProgressEvent.PROGRESS,progress);
			stream.addEventListener(Event.COMPLETE, complete);   
			stream.load(new URLRequest("icon.png"));   
		}   
		
		private function progress (event:ProgressEvent):void { 
			var stream:URLStream = event.target as URLStream;
			var bytes:ByteArray = new ByteArray();
			stream.readBytes(bytes);
			
			_loader.loadBytes(bytes);
		}   
		private function complete (event:Event):void {   
			trace("流加载完成！")
		}   
		private function loaderComplete (event:Event):void {   
			var bitmap:Bitmap = event.target.content as Bitmap;
			var bmp:BitmapData = bitmap.bitmapData;
			var bytes:ByteArray = BitmapBytes.encodeByteArray(bmp);
			bytes.uncompress();
			
			bytes.position = bytes.length - 1;
			var transparent:Boolean = bytes.readBoolean();
			bytes.position = bytes.length - 3;
			var height:int = bytes.readShort();
			bytes.position = bytes.length - 5;
			var width:int = bytes.readShort();
			
			trace(transparent,height,width)
			
			addChild(bitmap);
		}   

	}
}